package com.example.tester.controller;

import com.example.tester.annotation.LogWeb;
import com.example.tester.entity.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

/**
 * @author lzh
 * @date 2019/9/10 - 8:58
 */
@RestController
@Validated
@RequestMapping("user")
public class TestController {

    @GetMapping("test1")
    @LogWeb("后端校验test")
    public String test1(@NotBlank(message = "{required}") String name,
                        @Email(message = "{invalid}")  String  email
                        ){
        return "success";
    }


    /**
     * 参数很多时
     * 1.先在pojo对象中加@NotNull \@NotEmpty \@NotBlank 等校验
     * 2.然后在 控制器方法的参数名前 加@Valid 表示启用校验
     *
     * @param user
     * @return
     */
    @GetMapping("test2")
    public String test2(@Valid User user) {
        return "success";
    }





    @GetMapping("/{id:\\d+}")
    public void get(@PathVariable String id) {
        throw new RuntimeException("user not exist");
    }





}
